﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Data;
using centraldba_library.Data;

namespace centraldba_library.Admin
{
    public class msx_manager
    {
        #region " Fields "

        private msx_server _msx_server;

        public event SqlInfoMessageEventHandler InfoMessage;
        public event EventHandler ProcessComplete;

        #endregion

        #region " Constructor "

        public msx_manager(msx_server msxServer)
        {
            _msx_server = msxServer;
        }

        #endregion

        #region " Methods "

        public bool TestConnection()
        {
            SqlCommand cm = null;
            bool retval = true;

            try
            {
                // Does the connection to the server succeed?
                cm = SqlHelper.GetCommand(_msx_server.connection_string);
            }
            catch
            {
                // Release Objects
                SqlHelper.CloseConnection(cm);

                // Try to connect to the master database
                try
                {
                    cm = SqlHelper.GetCommand(_msx_server.connection_string_masterdb);
                }
                catch { retval = false; }
            }
            finally
            {
                // Release Objects
                SqlHelper.CloseConnection(cm);
            }

            // Return the status
            return retval;
        }

        public ObservableCollection<target_group> MSX_Groups()
        {
            // Declare Local Variables
            ObservableCollection<target_group> groups = null;
            SqlCommand cm = null;
            SqlDataReader dr = null;
            target_group group;

            try
            {
                // Get the data
                cm = SqlHelper.GetCommand(_msx_server.connection_string, "[dbo].[spMSX_msx_groups]", CommandType.StoredProcedure);
                dr = cm.ExecuteReader();

                while (dr.Read())
                {
                    group = new target_group(_msx_server);
                    group.server_group_id = dr.GetInt32(0);
                    group.group_name = dr.GetString(1);
                    groups.Add(group);
                    group = null;
                }
            }
            finally
            {
                // Release Objects
                SqlHelper.CloseConnection(cm, dr);
            }

            // Return the List of Groups
            return groups;
        }

        public ObservableCollection<target_server> MSX_Group_ServerMembers(Int32 servergroup_id)
        {
            // Declare Local Variables
            ObservableCollection<target_server> servers = null;
            SqlCommand cm = null;
            SqlDataReader dr = null;
            target_server server;

            try
            {
                // Get the data
                cm = SqlHelper.GetCommand(_msx_server.connection_string, "[dbo].[spMSX_msx_group_servermembers]", CommandType.StoredProcedure);
                if (servergroup_id > 0)
                    cm.Parameters.Add(new SqlParameter("servergroup_id", servergroup_id));
                dr = cm.ExecuteReader();

                while (dr.Read())
                {
                    server = new target_server();
                    server.server_group_id = dr.GetInt32(0);
                    server.server_id = dr.GetInt32(1);
                    server.server_name = dr.GetString(2);
                    server.enlist_date = dr.GetDateTime(3);
                    server.last_poll_date = dr.GetDateTime(4);
                    servers.Add(server);
                    server = null;
                }
            }
            finally
            {
                // Release Objects
                SqlHelper.CloseConnection(cm, dr);
            }

            // Return the List of Groups
            return servers;
        }

        public ObservableCollection<target_server> MSX_Servers_NoGroup()
        {
            // Declare Local Variables
            ObservableCollection<target_server> servers = null;
            SqlCommand cm = null;
            SqlDataReader dr = null;
            target_server server;

            try
            {
                // Get the data
                cm = SqlHelper.GetCommand(_msx_server.connection_string, "[dbo].[spMSX_msx_servers_nogroup]", CommandType.StoredProcedure);
                dr = cm.ExecuteReader();
                
                while (dr.Read())
                {
                    server = new target_server();
                    server.server_group_id = dr.GetInt32(0);
                    server.server_id = dr.GetInt32(1);
                    server.server_name = dr.GetString(2);
                    server.enlist_date = dr.GetDateTime(3);
                    server.last_poll_date = dr.GetDateTime(4);
                    servers.Add(server);
                    server = null;
                }
            }
            finally
            {
                // Release Objects
                SqlHelper.CloseConnection(cm, dr);
            }

            // Return the List of Groups
            return servers;
        }

        #endregion
    }
}
